package com.awise.app.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.awise.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/8.
 */

public class ItemAdapter extends BaseAdapter {
    private List<Map<String, Object>> data_list;
    private Context context;
    private int pos;

    public  ItemAdapter(Context context,ArrayList<Map<String, Object>> data_list){
        this.data_list=data_list;
        this.context=context;

    }

    public void setPosition(int  position){
        pos=position;
    }

    @Override
    public int getCount() {
        return data_list.size()==0?0:data_list.size();
    }

    @Override
    public Object getItem(int position) {
        return data_list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.home_adapter_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }    else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.image_main_pic.setImageResource((int)data_list.get(position).get("image"));
        holder.tv_title.setText(data_list.get(position).get("text")+"");

        AnimationSet animationSet = new AnimationSet(true);
        ScaleAnimation scaleAnimation = new ScaleAnimation(1,0.5f,1,0.5f,
                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        //0.5秒完成动画
        scaleAnimation.setDuration(500);
        //将AlphaAnimation这个已经设置好的动画添加到 AnimationSet中
        scaleAnimation.setRepeatCount(Integer.MAX_VALUE);//动画的重复次数
        animationSet.addAnimation(scaleAnimation);
        //启动动画
        holder.image_scale.startAnimation(animationSet);
//        holder.image_scale.setVisibility(position==pos?View.VISIBLE:View.GONE);
        return convertView;
    }

    static class ViewHolder{
        ImageView image_main_pic;
        ImageView image_scale;
        TextView tv_title;
        public ViewHolder(View view){
            image_main_pic = (ImageView) view.findViewById(R.id.image_main_pic);
            image_scale = (ImageView) view.findViewById(R.id.image_scale);
            tv_title = (TextView) view.findViewById(R.id.tv_title);
        }
    }
}
